﻿//===================================================================================
// Copyright (c) Ryan Garrett.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================

using System;
using System.Collections.Generic;
using System.Linq;

namespace Caffeine.Core.Test.TestModel.PropertyRegistrations
{
    public class ModelForComputedPropertyTestC : ModelBase
    {
        private readonly ModelProperty<Nullable<Int32>> _firstThreeOfSocial;
        private readonly ModelProperty<Nullable<Int32>> _middleTwoOfSocial;
        private readonly ModelProperty<Nullable<Int32>> _lastFourOfSocial;
        private readonly ModelProperty<String> _socialSecurityNumber;
        private readonly ModelProperty<String> _propertyA;
        private readonly ModelProperty<String> _propertyB;
        private readonly ModelProperty<String> _propertyC;

        public ModelForComputedPropertyTestC()
        {
            _firstThreeOfSocial = RegisterProperty<Nullable<Int32>>("FirstThreeOfSocial");
            _middleTwoOfSocial = RegisterProperty<Nullable<Int32>>("MiddleTwoOfSocial");
            _lastFourOfSocial = RegisterProperty<Nullable<Int32>>("LastFourOfSocial");
            _socialSecurityNumber = RegisterComputedProperty<String, Nullable<Int32>>("SocialSecurityNumber", ComputeSocialSecurityNumber, FirstThreeOfSocial, MiddleTwoOfSocial, LastFourOfSocial);
            _propertyA = RegisterProperty<String>("PropertyA");
            _propertyB = RegisterComputedProperty<String>("PropertyB", ComputePropertyB, PropertyA);
            _propertyC = RegisterComputedProperty<String>("PropertyC", ComputePropertyC, PropertyB);
        }

        public ModelProperty<Nullable<Int32>> FirstThreeOfSocial
        {
            get
            {
                return _firstThreeOfSocial;
            }
        }

        public ModelProperty<Nullable<Int32>> MiddleTwoOfSocial
        {
            get
            {
                return _middleTwoOfSocial;
            }
        }

        public ModelProperty<Nullable<Int32>> LastFourOfSocial
        {
            get
            {
                return _lastFourOfSocial;
            }
        }

        public ModelProperty<String> SocialSecurityNumber
        {
            get
            {
                return _socialSecurityNumber;
            }
        }

        public ModelProperty<String> PropertyA
        {
            get
            {
                return _propertyA;
            }
        }

        public ModelProperty<String> PropertyB
        {
            get
            {
                return _propertyB;
            }
        }

        public ModelProperty<String> PropertyC
        {
            get
            {
                return _propertyC;
            }
        }
        
        private static String ComputeSocialSecurityNumber(IEnumerable<ModelProperty<Nullable<Int32>>> properties, Nullable<Int32> newPropertyValue)
        {
            return String.Format
            (
                "{0}-{1}-{2}",
                properties.ElementAt(0),
                properties.ElementAt(1),
                properties.ElementAt(2)
            );
        }

        private static String ComputePropertyB(IEnumerable<ModelProperty<String>> properties, String newPropertyValue)
        {
            return String.Format("B - {0}", properties.ElementAt(0));
        }

        private static String ComputePropertyC(IEnumerable<ModelProperty<String>> properties, String newPropertyValue)
        {
            return String.Format("C - {0}", properties.ElementAt(0));
        }
    }
}
